<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="用户级别" prop="userLevel">
        <el-input
          v-model="queryParams.userLevel"
          placeholder="请输入用户级别"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:passenger-info:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:passenger-info:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键ID" align="center" prop="id" width="80" />
      <el-table-column label="用户ID" align="center" prop="userId" width="100" />
      <el-table-column label="用户名" align="center" prop="username" width="120">
        <template #default="scope">
          {{ scope.row.username || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile" width="120">
        <template #default="scope">
          {{ scope.row.mobile || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="真实姓名" align="center" prop="realName" width="120">
        <template #default="scope">
          {{ scope.row.realName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickname" width="120">
        <template #default="scope">
          {{ scope.row.nickname || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center" prop="email" width="160">
        <template #default="scope">
          {{ scope.row.email || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idCard" width="160">
        <template #default="scope">
          {{ scope.row.idCard || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="用户级别" align="center" prop="userLevel" width="100">
        <template #default="scope">
          {{ scope.row.userLevel || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="成长值" align="center" prop="growthValue" width="80">
        <template #default="scope">
          {{ scope.row.growthValue || 0 }}
        </template>
      </el-table-column>
      <el-table-column label="总出行里程" align="center" prop="totalKilometers" width="120">
        <template #default="scope">
          {{ scope.row.totalKilometers ? scope.row.totalKilometers + ' 公里' : '-' }}
        </template>
      </el-table-column>
      <el-table-column label="总充值金额" align="center" prop="totalRechargeAmount" width="120">
        <template #default="scope">
          <span style="color: #67C23A;">¥{{ formatMoney(scope.row.totalRechargeAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总支付金额" align="center" prop="totalPayAmount" width="120">
        <template #default="scope">
          <span style="color: #E6A23C;">¥{{ formatMoney(scope.row.totalPayAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template #default="scope">
          {{ formatDateTime(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="info"
            @click="openDetail(scope.row)"
            v-hasPermi="['system:passenger-info:query']"
          >
            详情
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:passenger-info:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:passenger-info:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <PassengerInfoForm ref="formRef" @success="getList" />
  
  <!-- 详情弹窗 -->
  <PassengerInfoDetail ref="detailRef" />
</template>

<script setup lang="ts">
import { formatDate } from '@/utils/formatTime'
import { getPassengerInfoPage, deletePassengerInfo, exportPassengerInfo } from '@/api/system/passengerinfo'
import PassengerInfoForm from './PassengerInfoForm.vue'
import PassengerInfoDetail from './PassengerInfoDetail.vue'
import download from '@/utils/download'

/** 乘客信息 列表 */
defineOptions({ name: 'SystemPassengerInfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: undefined,
  username: undefined,
  nickname: undefined,
  realName: undefined,
  mobile: undefined,
  userLevel: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getPassengerInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 详情操作 */
const detailRef = ref()
const openDetail = (row: any) => {
  detailRef.value.open(row)
}

/** 格式化日期 */
const formatDateTime = (date: string | Date) => {
  if (!date) return '-'
  const dateObj = typeof date === 'string' ? new Date(date) : date
  return formatDate(dateObj, 'YYYY-MM-DD HH:mm:ss')
}

/** 格式化金额 */
const formatMoney = (amount: number | string) => {
  if (!amount && amount !== 0) return '0.00'
  return Number(amount).toFixed(2)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await deletePassengerInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await exportPassengerInfo(queryParams)
    download.excel(data, '乘客信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>